home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Dr. Windows 3
/
dr win3.zip
/
dr win3
/
WINSOUND
/
RANDYSND.ZIP
/
RANDYSND.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1992-06-12
|
16KB
|
631 lines
/* randysnd.cpp */
// RandySnd is (C) 1992 Anthony McCarthy
//
// Feel free to use and abuse this program to your own ends.
// However, it is provided FREE, AS-IS and with absolutely NO
// WARRANTY whatsoever. Use it at your own risk.
//
// The above statement is especially appropriate as this is
// my very first program built using MSC++ 7 and Microsoft's
// Foundation Classes. As yet I haven't really got my head
// around how to play with precompiled headers. Also, I ain't
// yet played with the diagnostic services provided by MFC
// and so there may well be memory leaks that I haven't seen
// yet.
//
// Please forgive the proliferation of literal integers and
// strings in the code - this code was a quick hack - they
// should all really be symbolic at least. Also, I haven't yet
// developed/decided on a personal coding style for C++ so
// things might feel inconsistent.
//
// If you really like this program, a donation of a few pounds,
// dollars, roubles, francs or whatever would be appreciated, to:
// Anthony McCarthy, 14 Beryl Road, Bedminster, Bristol, BS3 3DH, UK
//
// Having said there's no warranty, please report any bugs, enhancement
// requests or source code criticisms to:
// 100012.3712@compuserve.com
// amccarthy@cix.compulink.co.uk
// amc@beryl.demon.co.uk
// CAVEAT: if you're trying to recompile this source with the same
// release of MSC 7.0 as I'm using, the compile will fail. Microsoft
// failed to include the AfxSig_vh enum in afxmsg.h although it is
// referenced in the ON_WM_DROPFILES() macro in the same file! To work
// around this, I simply changed 'AfxSig_vh' to 'AfxSig_vw' in a copy
// of afxmsg.h.
// Change History
// ~~~~~~~~~~~~~~
//
// v1.00 : AMcC : 10June92 : initial creation
# include <afxwin.h>
# include <afxcoll.h>
# include <mmsystem.h>
# include <commdlg.h>
# include <shellapi.h>
# include <stdlib.h>
# include "randysnd.h"
static char scratchBuffer[512];
/*--------------------------------------------------------------------------*/
/* */
/* C M a i n W i n d o w */
/* */
/*--------------------------------------------------------------------------*/
class CMainDialog : public CModalDialog
{
private:
CPtrList soundsList;
BOOL bFilesChanged,
bAutoRandomiseChanged;
CString curSysSound;
WORD templateId;
public:
static const char * iniFileName;
CMainDialog(WORD dlgId) : CModalDialog(dlgId) { bFilesChanged = FALSE;
bAutoRandomiseChanged = FALSE;
templateId = dlgId;
}
~CMainDialog();
BOOL OnInitDialog();
BOOL GetIniString(CString section, CString key, CString& value);
BOOL LoadWavFilesList(const CString & sysSound);
BOOL SaveWavFilesList(const CString & sysSound);
BOOL FlushWavFilesList();
BOOL GetSysSound(CString& sysSound);
BOOL TryToRandomizeSysSound(CString sysSound);
afx_msg void OnAbout();
afx_msg void OnDestroy();
afx_msg void OnSysSndSelChange();
afx_msg void OnFileSelChange();
afx_msg void OnPlaySound();
afx_msg void OnAddSoundFile();
afx_msg void OnRemoveSoundFile();
afx_msg void OnAutoRandomise();
afx_msg void OnRandomiseNow();
afx_msg void OnDropFiles(HDROP hDrop);
DECLARE_MESSAGE_MAP()
};
/*--------------------------------------------------------------------------*/
/* */
/* C M y A p p */
/* */
/*--------------------------------------------------------------------------*/
class CMyApp : public CWinApp
{
public:
int Run();
BOOL WantedMinimize();
};
BOOL CMyApp::WantedMinimize()
{
return ( m_nCmdShow == SW_SHOWMINIMIZED
|| m_nCmdShow == SW_MINIMIZE
|| m_nCmdShow == SW_SHOWMINNOACTIVE );
}
int CMyApp::Run()
{
WORD dlgId;
if (WantedMinimize())
dlgId = IDD_WORKINPROGRESS;
else
dlgId = IDD_MAIN;
CMainDialog mainDialog(dlgId);
mainDialog.DoModal();
return 0;
}
// Static application object... the constructor for
// this kicks everything off.
//
// This comment is here because even though I find the
// concept of static intializers simple enough to comprehend,
// its still damned difficult to see where the app 'gets going'.
CMyApp myApp;
const char * CMainDialog::iniFileName = "randysnd.ini";
BEGIN_MESSAGE_MAP( CMainDialog, CModalDialog )
ON_COMMAND( ID_ABOUT , OnAbout )
ON_COMMAND( ID_PLAYSOUNDFILE , OnPlaySound )
ON_COMMAND( ID_ADDSOUNDFILE , OnAddSoundFile )
ON_COMMAND( ID_REMOVESOUNDFILE, OnRemoveSoundFile )
ON_COMMAND( ID_AUTORANDOMISE , OnAutoRandomise )
ON_COMMAND( ID_RANDOMIZENOW , OnRandomiseNow )
ON_WM_DESTROY( )
ON_WM_DROPFILES( ) // *SIGH* MS omitted the AfxSig_vh enum from afxmsg.h!
// To work round this, I modified the ON_WM_DROPFILES()
// macro (in afxmsg.h) to use the AfxSig_vw signature.
ON_CBN_SELCHANGE( ID_SYSTEMSOUNDS, OnSysSndSelChange )
ON_LBN_SELCHANGE( ID_FILESLIST , OnFileSelChange )
END_MESSAGE_MAP()
CMainDialog::~CMainDialog()
{
CString * pCstr;
while ( ! soundsList.IsEmpty()
&& (pCstr = (CString *)soundsList.RemoveHead()) != NULL)
delete pCstr;
}
BOOL CMainDialog::GetSysSound(CString& sysSound)
{
BOOL ok = FALSE;
CComboBox * cb = (CComboBox *)GetDlgItem(ID_SYSTEMSOUNDS);
CString * s = (CString *)(soundsList.GetAt(soundsList.FindIndex(cb->GetCurSel())));
if (! soundsList.IsEmpty())
{
sysSound = *s;
ok = TRUE;
}
return ok;
}
BOOL CMainDialog::GetIniString(CString section, CString key, CString& value)
{
BOOL ok;
ok = GetPrivateProfileString( section,
key,
"",
value.GetBuffer(256),
256,
iniFileName ) > 0;
value.ReleaseBuffer();
return ok;
}
BOOL CMainDialog::FlushWavFilesList()
{
BOOL ok = TRUE;
if (bFilesChanged || bAutoRandomiseChanged)
if (! curSysSound.IsEmpty())
SaveWavFilesList(curSysSound);
return ok;
}
BOOL CMainDialog::LoadWavFilesList(const CString& sysSound)
{
BOOL ok = TRUE;
char str[20];
int ctr;
CListBox * lb = (CListBox *)GetDlgItem(ID_FILESLIST);
CButton * but = (CButton *)GetDlgItem(ID_AUTORANDOMISE);
CString wavName;
FlushWavFilesList();
lb->ResetContent();
int qty = GetPrivateProfileInt(sysSound, "FileCount", 0, iniFileName);
for ( ctr = 1;
ctr <= qty;
ctr++ )
{
wsprintf(str, "SoundFile%d", ctr);
if (GetIniString(sysSound, str, wavName))
lb->AddString(wavName);
}
but->SetCheck(GetPrivateProfileInt(sysSound, "Randomize", 0, iniFileName));
curSysSound = sysSound;
bFilesChanged = FALSE;
bAutoRandomiseChanged = FALSE;
return ok;
}
BOOL CMainDialog::SaveWavFilesList(const CString & sysSound)
{
BOOL ok = TRUE;
int qty = 0;
int count = 0;
CListBox * lb = (CListBox *)GetDlgItem(ID_FILESLIST);
char key[30];
char value[10];
CString filename;
CButton * but = (CButton *)GetDlgItem(ID_AUTORANDOMISE);
qty = lb->GetCount();
if (qty == LB_ERR)
qty = 0;
for ( count = 1;
count <= qty;
count++ )
{
wsprintf(key, "SoundFile%d", count);
lb->GetText(count - 1, filename);
WritePrivateProfileString(sysSound, key, filename, iniFileName);